package com.fzkj.shejimoshi.dynamicproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @DESCRIPTION: TODO 用于创建代理类
 * @AUTHOR: AnotherOne
 * @DATE: 2021/8/16 11:41
 */
public class ProxyFactory {

    // 代理对象
    private Object target;

    public ProxyFactory(Object target){
        this.target = target;
    }

    // 创建代理对象
    public Object getProxy(){
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("=== 前置处理 ===");
                        method.invoke(target, args);
                        System.out.println("=== 后置处理 ===");
                        return null;
                    }
                });
    }
}
